<?php

declare(strict_types=1);

namespace app\user\controller\api;

use sheep\controller\Api;
use think\Request;
use sheep\facade\Sms;
use app\user\model\User as UserModel;
use sheep\model\ThirdOauth;

class User extends Api
{

    // 用户信息
    public function profile()
    {
        $userId = $this->auth()->user()->id;
        $user = UserModel::with(['parent_user', 'third_oauth'])->findOrFail($userId);
        $user->hidden(['password', 'salt', 'create_time', 'update_time', 'delete_time', 'remember_token', 'login_fail', 'login_ip', 'login_time']);

        return success('个人详情', $user);
    }

    // 用户信息
    public function update()
    {
        $params = $this->request->only(['avatar', 'nickname', 'gender']);
        $user = $this->auth()->user();
        $this->svalidate($params, '\app\user\validate\User');

        $user->save($params);
        $user->hidden(['password', 'salt', 'create_time', 'update_time', 'delete_time', 'remember_token', 'login_fail', 'login_ip', 'login_time']);
        return success('更新成功', $user);
    }

    // 1账号密码登录
    public function accountLogin(Request $request)
    {
        if ($this->auth()->user()) {
            return error('您已登录,不需要重新登录');
        }

        $params = $request->only(['account', 'password']);
        $this->svalidate($params, '.accountLogin');

        if ($this->auth()->attempt($params, true)) {
            $user = $this->auth()->user();
            if ($user) return success('登录成功');
        }
        return error('登录失败');
    }

    // 2短信验证码登陆
    public function smsLogin(Request $request)
    {
        if ($this->auth()->user()) {
            return error('您已登录,不需要重新登录');
        }

        $params = $request->only(['mobile', 'code']);
        $this->svalidate($params, '\app\user\validate\User.smsLogin');

        Sms::check($params['mobile'], 'smsLogin', $params['code']);

        $user = UserModel::getByMobile($params['mobile']);
        if (!$user) {
            return error('该手机号还没有注册');
        }

        $user = $this->auth()->login($user)->user();
        if ($user) {
            return success('登录成功');
        }
        return error('登录失败');
    }

    // 3短信验证码注册
    public function smsRegister(Request $request)
    {
        if ($this->auth()->user()) {
            return error('您已登录,请先退出登录');
        }

        $params = $request->only(['mobile', 'code', 'password']);
        $this->svalidate($params, '.smsRegister');

        Sms::check($params['mobile'], 'smsRegister', $params['code']);

        // 注册
        $user = $this->auth()->register($params);

        // 模拟登录
        $this->auth()->login($user);

        return success('注册成功');
    }

    // 4修改密码
    public function changePassword(Request $request)
    {
        $params = $request->only(['oldPassword', 'newPassword']);
        $this->svalidate($params, '.changePassword');

        $this->auth()->modifyPassword($params['oldPassword'], $params['newPassword']);

        return success('修改成功');
    }

    // 5重置/忘记密码
    public function resetPassword(Request $request)
    {
        $params = $request->only(['mobile', 'code', 'password']);
        $this->svalidate($params, '.resetPassword');

        Sms::check($params['mobile'], 'resetPassword', $params['code']);

        $this->auth()->resetPassword($params);

        return success('修改成功');
    }

    // 6更换手机号
    public function changeMobile(Request $request)
    {
        $user = $this->auth()->user(true);

        $params = $request->only(['mobile', 'code']);
        $this->svalidate($params, '.changeMobile');

        Sms::check($params['mobile'], 'changeMobile', $params['code']);

        $user->mobile = $params['mobile'];
        $user->save();

        return success('绑定成功');
    }

    // 7修改用户名
    public function changeUsername(Request $request)
    {
        $user = $this->auth()->user(true);

        $params = $request->only(['username']);
        $this->svalidate($params, '.changeUsername');

        $user->username = $params['username'];
        $user->save();
        return success('绑定成功');
    }

    // 登出
    public function logout()
    {
        $this->auth()->user(true);
        $this->auth()->logout();

        return success('退出成功');
    }

    // 第三方授权信息
    public function thirdOauth(Request $request)
    {
        $user = $this->auth()->user(true);

        $provider = $request->param('provider', '');
        $platform = $request->param('platform', '');
        if (!in_array($platform, ['miniProgram', 'officialAccount', 'openPlatform'])) {
            return error('参数有误');
        }
        $where = [
            'platform' => $platform,
            'user_id' => $user->id
        ];
        if ($provider !== '') {
            $where['provider'] = $provider;
        }
        $oauth = ThirdOauth::where($where)->field('nickname, avatar, platform, provider')->find();
        return success('', $oauth);
    }


    /**
     * 用户注销
     *
     * @return void
     */
    public function logoff()
    {
        $user = $this->auth()->user();

        // 删除用户
        $user->delete();
        // 登出 & 销毁token
        $this->auth()->logout();

        return success('注销成功');
    }
}
